package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.*;
import coms.dto.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import coms.interceptors.UserAware;
import java.util.Map;

public class PublicationAction extends ComsActionSupport implements IAction, UserAware {

    private User user;
    private int publicationId;
    private Publication publication;
    private List<Publication> publicationList;
    private PublicationDelegate publicationDelegate;
    private List<ConferenceItem> conferenceItemList;
    private ConferenceItemDelegate conferenceItemDelegate;
    private List<PresentationType> presentationTypeList;
    private PresentationTypeDelegate presentationTypeDelegate;
    private List<User> userList;
    private UserDelegate userDelegate;
    private String action;
    private boolean admin;

    public boolean isAdmin() {
         User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public PublicationAction() {
        publication = new Publication();
        publicationList = new ArrayList<Publication>();
        publicationDelegate = new PublicationDelegate();

        conferenceItemList = new ArrayList<ConferenceItem>();
        conferenceItemDelegate = new ConferenceItemDelegate();

        presentationTypeList = new ArrayList<PresentationType>();
        presentationTypeDelegate = new PresentationTypeDelegate();

        userList = new ArrayList<User>();
        userDelegate = new UserDelegate();

    }

    private void initForm() {
        try {
            Map<String, Object> user = getSession();
            User u = (User) user.get("user");
            if (u != null) {
                admin = u.isAdmin();
            }
            conferenceItemList = conferenceItemDelegate.findAll();
            presentationTypeList = presentationTypeDelegate.findAll();
            userList = userDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateDefault() {
        publication = new Publication();
        Map<String, Object> sessionMap = getSession();
        User sessionUser = (User) sessionMap.get("user");

        if (sessionUser != null) {
            publication.setFirstAuthor(sessionUser);
            admin = sessionUser.isAdmin();
        }
    }

    private void populate() {
        Map<String, Object> sessionMap = getSession();
        User u = (User) sessionMap.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }

        try {
            publication = publicationDelegate.getById(publicationId);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {
        initForm();
        populateDefault();
        action = "createPublication_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updatePublication_" + publication.getId();
        return "edit";
    }

    @Override
    public String create() {

        publicationDelegate.create(publication);
        return Action.SUCCESS;


    }

    @Override
    public String update() {
        Map<String, Object> sessionMap = getSession();
        User u = (User) sessionMap.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }

        try {
            publicationDelegate.update(publication);
        } catch (Exception ex) {
            return "error";
        }

        return "updated";
    }

    @Override
    public String delete() {
        populate();
        publicationDelegate.delete(publication);
        return "deleted";
    }

    @Override
    public String findAll() {
        Map<String, Object> sessionMap = getSession();
        User u = (User) sessionMap.get("user");
        if (u != null) {
            admin = u.isAdmin();
        }

        try {
            publicationList = publicationDelegate.findAll();
        } catch (Exception ex) {
            return "error";
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {

        initForm();
        try {
            if (publication == null) {
                publicationList = publicationDelegate.findAll();
            } else {
                publicationList = publicationDelegate.search(publication);
            }
        } catch (Exception ex) {
            Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "search";
    }

    public List<ConferenceItem> getConferenceItemList() {
        return conferenceItemList;
    }

    public void setConferenceItemList(List<ConferenceItem> conferenceItemList) {
        this.conferenceItemList = conferenceItemList;
    }

    public List<PresentationType> getPresentationTypeList() {
        return presentationTypeList;
    }

    public void setPresentationTypeList(List<PresentationType> presentationTypeList) {
        this.presentationTypeList = presentationTypeList;
    }

    public Publication getPublication() {
        return publication;
    }

    public void setPublication(Publication publication) {
        this.publication = publication;
    }

    public List<Publication> getPublicationList() {
        return publicationList;
    }

    public void setPublicationList(List<Publication> publicationList) {
        this.publicationList = publicationList;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }

    public void setFirstAuthor(String id) {
        System.out.println("###id =" + id);
        if (id == null || id.equals("")) {
            return;
        }
        try {
            int firstAuthorId = Integer.parseInt(id);
            User firstAuthor = userDelegate.getById(firstAuthorId);
            publication.setFirstAuthor(firstAuthor);
        } catch (Exception ex) {
            Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setConferenceItem(String id) {
        if (id == null || id.equals("")) {
            return;
        }
        try {
            System.out.println("conference item id" + id);
            int conferenceItemId = Integer.parseInt(id);
            ConferenceItem conferenceItem = conferenceItemDelegate.getById(conferenceItemId);
            publication.setConferenceItem(conferenceItem);
        } catch (Exception ex) {
            Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void setPresentationType(String id) {
        if (id == null || id.equals("")) {
            return;
        }
        try {
            int presentationTypeId = Integer.parseInt(id);
            PresentationType presentationType = presentationTypeDelegate.getById(presentationTypeId);
            publication.setPresentationType(presentationType);
            System.out.println("presentationType" + presentationType.getId());
        } catch (Exception ex) {
            Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getPublicationId() {
        return "" + publicationId;
    }

    public void setPublicationId(String publicationId) {
        try {
            if (publicationId != null && !publicationId.equals("")) {
                this.publicationId = Integer.parseInt(publicationId);
                publication.setId(this.publicationId);
            }
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public User getUser() {
        return this.user;
    }

    @Override
    public void setUser(User user) {
        this.user = user;
    }

    public String viewMy() {

        Map<String, Object> sessionMap = getSession();
        User sessionUser = (User) sessionMap.get("user");
        try {
            publicationList = publicationDelegate.getByAnyAuthorType(sessionUser);
            admin=sessionUser.isAdmin();
        } catch (Exception ex) {
            return "error";
        }

        return "searchMy";
    }

    public String getKeywords() {

        if (publication == null || publication.getKeywords() == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (String keyword : publication.getKeywords()) {
            sb.append(keyword).append(";");
        }
        return sb.toString();


    }

    public void setKeywords(String keywords) {
        String[] selectedKeywordList = keywords.split(";");
        List<String> keywordList = new ArrayList<String>();
        for (String selectedKeyword : selectedKeywordList) {
            if (!selectedKeyword.equals("")) {
                keywordList.add(selectedKeyword);
            }
        }
        publication.setKeywords(keywordList);
    }

    public String getCoauthors() {

        if (publication == null || publication.getCoAuthor() == null) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (User coauthor : publication.getCoAuthor()) {
            sb.append(coauthor.getId()).append(":").append(coauthor.getFullname()).append(";");
        }
        return sb.toString();


    }

    public void setCoauthors(String coauthors) {
        String[] selectedCoauthorList = coauthors.split(";");
        List<User> coauthorList = new ArrayList<User>();
        for (String selectedCoauthor : selectedCoauthorList) {
            if (!selectedCoauthor.equals("")) {
                try {
                    coauthorList.add(userDelegate.getById(Integer.parseInt(selectedCoauthor.split(":")[0])));
                } catch (Exception ex) {
                    Logger.getLogger(PublicationAction.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        publication.setCoAuthor(coauthorList);
    }
}
